package com.kuang.thread;

import java.util.concurrent.*;

/**
 *Callable<Boolean>表示接口需要实现的Call()方法返回值类型
 */
public class TestCallable01 implements Callable<Boolean> {

    private String url; //网络我图片地址
    private String FileName;   //保存的文件名

    public TestCallable01(String url, String FileName){
        this.url = url;
        this.FileName = FileName;
    }

    //下载图片线程的执行体

    public Boolean call() throws Exception {
        WebDownloader webDownloader = new WebDownloader();
        try {
            webDownloader.downloader(url,FileName);
            System.out.println("下载的文件名：" + FileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        TestCallable01 t1 = new TestCallable01("https://www.baidu.com/img/baidu_jgylogo3.gif","copy1.png");
        TestCallable01 t2 = new TestCallable01("https://www.baidu.com/img/baidu_resultlogo@2.png","copy2.png");
        TestCallable01 t3 = new TestCallable01("https://www.baidu.com/img/bd_logo1.png","copy3.png");

        //1：创建执行服务：
        //param: 有创建三个线程
        ExecutorService ser = Executors.newFixedThreadPool(3);
        //2：提交执行
        Future<Boolean> r1 = ser.submit(t1);
        Future<Boolean> r2 = ser.submit(t2);
        Future<Boolean> r3 = ser.submit(t3);
        //3：获取结果
        boolean rs1 = r1.get();
        boolean rs2 = r2.get();
        boolean rs3 = r3.get();

        System.out.println(rs1);
        System.out.println(rs2);
        System.out.println(rs3);
        //4：关闭服务
        ser.shutdown();
    }
}

